import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:yysc_flutter/page/home_page.dart';
import 'package:yysc_flutter/page/home_page1.dart';
import 'package:yysc_flutter/page/home_page2.dart';
import 'package:yysc_flutter/page/home_page3.dart';
import 'package:yysc_flutter/page/home_page4.dart';
import 'package:yysc_flutter/util/CacheUtil.dart';
import 'package:yysc_flutter/util/CommonUtils.dart';
import 'package:yysc_flutter/util/MyRoute.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  /// 初始化缓存框架
  await CacheUtil.instance.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: MyRoute.getRouter(),
      title: "小茶应用",
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late PackageInfo _packageInfo;
  late final PageController _pageController = PageController();
  late int _currentIndex = 0;
  late String _rate = "0";

  @override
  void initState() {
    super.initState();
    _initPackageInfo();
    CommonUtils.getConfig((data){
      String androidAppkey = data['data']['umengAndroid'];
      String iosAppkey = data['data']['umengIos'];
      CommonUtils.initUmeng(androidAppkey,iosAppkey);
    });

  }

  Future<void> _initPackageInfo() async {
    _packageInfo = await PackageInfo.fromPlatform();
    CommonUtils.check(_packageInfo.version, (data) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        CommonUtils.showUpdateDialog(context, data, (rate) {
          setState(() {
            _rate = rate;
          });
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        scrollDirection: Axis.horizontal,
        onPageChanged: (page) {
          setState(() {
            _currentIndex = page;
          });
        },
        children: <Widget>[
          HomePage(),
          HomePage1(
            data: "abc",
          ),
          HomePage2(),
          HomePage3(),
          HomePage4(),
        ],
      ),
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
          splashFactory: NoSplash.splashFactory,
        ),
        child: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
            _pageController.animateToPage(index,
                duration: const Duration(milliseconds: 1),
                curve: Curves.easeInOut);
          },
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Icon(Icons.access_time), label: "Home"),
            BottomNavigationBarItem(
                icon: Icon(Icons.access_time), label: "Home"),
            BottomNavigationBarItem(
                icon: Icon(Icons.access_time), label: "Home"),
            BottomNavigationBarItem(
                icon: Icon(Icons.access_time), label: "Home"),
            BottomNavigationBarItem(
                icon: Icon(Icons.access_time), label: "Home"),
          ],
        ),
      ),
    );
  }
}
